package com.example.studentmanagmentsystem;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {

    private OnDeleteClickListener onDeleteClickListener;
    private OnQueryClickListener onQueryClickListener;

    public OnDeleteClickListener getOnDeleteClickListener() {
        return onDeleteClickListener;
    }

    public void setOnDeleteClickListener(OnDeleteClickListener onDeleteClickListener) {
        this.onDeleteClickListener = onDeleteClickListener;
    }

    public OnQueryClickListener getOnQueryClickListener() {
        return onQueryClickListener;
    }

    public void setOnQueryClickListener(OnQueryClickListener onQueryClickListener) {
        this.onQueryClickListener = onQueryClickListener;
    }

    private List<Student> studentList;

    static  class ViewHolder extends RecyclerView.ViewHolder{
        TextView studentId;
        TextView studentName;
        TextView studentSex;
        TextView studentEnter;
        Button queryStudentButton;
        Button deleteStudentButton;
        public ViewHolder(View view){
            super(view);
            studentId = (TextView) view.findViewById(R.id.student_id);
            studentName = (TextView) view.findViewById(R.id.student_name);
            studentEnter = (TextView) view.findViewById(R.id.student_enter_year);
            studentSex = (TextView) view.findViewById(R.id.student_sex);
            queryStudentButton = (Button) view.findViewById(R.id.student_query_score);
            deleteStudentButton = (Button) view.findViewById(R.id.delete_student);
        }
    }

    public StudentAdapter(List<Student> students){
        studentList = students;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.students_list, parent, false);
        final ViewHolder holder = new ViewHolder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Student student = studentList.get(position);
        holder.studentId.setText(student.getId() + "");
        holder.studentName.setText(student.getName());
        holder.studentEnter.setText(student.getEnterYear() + " ");
        if (student.isSex()){
            holder.studentSex.setText("男");
        }else {
            holder.studentSex.setText("女");
        }
        holder.deleteStudentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position_delete = holder.getLayoutPosition();
                //在StudentShowActivity中实现
                onDeleteClickListener.onDeleteClick(holder.itemView, position_delete);
            }
        });

        holder.queryStudentButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position_query = holder.getLayoutPosition();
                //在StudentShowActivity中实现
                onQueryClickListener.onQueryClick(holder.itemView, position_query);
            }
        });

    }

    @Override
    public int getItemCount() {
        return studentList.size();
    }

    public interface OnQueryClickListener {
        void onQueryClick(View view, int position);
    }

    public interface OnDeleteClickListener{
        void onDeleteClick(View view, int position);
    }
}
